//
//  SCChangeTPController.m
//  Scalper
//
//  Created by 王云 on 2017/9/28.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SCChangeTPController.h"

@interface SCChangeTPController ()<UITextFieldDelegate>

@end

@implementation SCChangeTPController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"设置新的手机";
    
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
//    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
//    textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
//    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
//    [self.navigationController.navigationBar setTitleTextAttributes:textAttrs];
//    UIBarButtonItem *backLfet = [UIBarButtonItem itemWithIcon:@"good_arrow_yellow" highIcon:@"good_arrow_yellow" target:self action:@selector(back)];
//    self.navigationItem.leftBarButtonItem = backLfet;
    
     _PromptL.attributedText = [WYNSString Att:@"温馨提示:\n如待绑定手机号已被其他账号绑定，您将不能绑定该手机号；如您确定需要将当前账号与该手机号做绑定，请返回登录界面，用该手机号登录黄牛派后，并在“我的-设置-绑定手机号”界面，换绑其他手机号，以将该手机号从系统中空出来即可。" rang:@"温馨提示:" font:[UIFont systemFontOfSize:15] color:KOrange ParagraphSpacing:8];
    
}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)verClick:(id)sender {
    
    [self.view endEditing:YES];
    __weak __typeof(&*self)weakSelf = self;
    if (![HWCommon isMobileNumber:_phoneF.text]) {
        [SCTool showError:@"请输入正确手机号"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.phoneF becomeFirstResponder];
        });
        return;
    }
    [SCApiTool ScCheckexist:_phoneF.text success:^(NSString *msg) {
        [self verCode];
        
    } failure:^(NSString *errormsg) {
        [SCTool showMessage:errormsg];
    }];
}
- (void)verCode{
    
    __block NSInteger timeout=59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    [SCApiTool ScSendPhone:_phoneF.text type:@"bind_mobile" success:^(NSString *verStr) {
        [SCTool showSuccess:verStr];
        
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(_timer, ^{
            if(timeout<=0){
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                    _verButton.userInteractionEnabled = YES;
                    [_verButton setBackgroundColor:KOrange];
                });
            }else{
                NSInteger seconds = timeout % 60;
                NSString *strTime = [NSString stringWithFormat:@"%.2ld", (long)seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_verButton setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                    _verButton.userInteractionEnabled = NO;
                    [_verButton setBackgroundColor:WYColor(215, 215, 215)];
                });
                timeout--;
                
            }
        });
        dispatch_resume(_timer);
        
    } failure:^(NSString *msg) {
        [SCTool showError:msg];
        [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _verButton.userInteractionEnabled = YES;
        _verButton.backgroundColor = KOrange;
    }];
    
}


- (IBAction)yesClick:(id)sender {
    
    [self.view endEditing:YES];
    __weak __typeof(&*self)weakSelf = self;
    if (![HWCommon isMobileNumber:_phoneF.text]) {
        [SCTool showError:@"请输入正确手机号"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.phoneF becomeFirstResponder];
        });
        return;
    }
    if (_verF.text.length == 0 ) {
        [SCTool showError:@"请输入验证码"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.verF becomeFirstResponder];
        });
        return;
    }
    
    [SCApiTool ScRevisemobilePhone:_phoneF.text code:_verF.text success:^(NSString *msg) {
        [SCTool showSuccess:msg];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        });
    } failure:^(NSString *errormsg) {
        [SCTool showError:errormsg];
    }];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _verButton.userInteractionEnabled = YES;
}
#pragma mark - textFiled delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == _phoneF){
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 11) {
            return NO;
        }
        
    }
    if (textField == _verF){
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 10) {
            return NO;
        }
        
    }
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
